/**
 * 文章评价管理
 * 
 * @param $
 */
(function($) {

	var ArticleCommentObject = {
		// 用于识别当前选中行
		data_index : "",

		init : function() {
			// 搜索事件
			this._reg_article_comment_search_btn();
			// 初始化文章评论表格
			this._init_table();
			// 关闭文章评论按钮事件
			this._reg_closed_comment_btn();
			// 删除文章提交按钮
			this._reg_delete_comment_btn();

		},

		/** 添加搜索事件 */
		_reg_article_comment_search_btn : function() {
			$("#pf-article-comment-search-btn").click(function() {
				ArticleCommentObject.searchStoreComment();
			});
			// 回车事件
			$("#pf-search-article-comment-key").keydown(function(e) {
				if (e.keyCode == 13) {
					ArticleCommentObject.searchStoreComment();
				}
			});
		},

		/** 搜索文章 */
		searchStoreComment : function() {
			// 刷新
			var data = {
				queryParams : function(params) {
					params.searchText = $("#pf-search-article-comment-key").val();
					params.closed = $("#pf-select-article-comment-closed").val();
					params.articleId = $("#select-article-comment-articleId").val();
					return params;
				},
				pageNumber : 1
			};
			$("#pf-article-comment-table").bootstrapTable('refresh', data);
		},

		/** 删除文章评论按钮事件 */
		_reg_delete_comment_btn : function() {
			$(document).on("click", ".pf-delete-comment-btn", function() {
				var $tr = $(this).closest("tr");
				layer.msg('确定删除评论内容为：' + $tr.data("data").content + "的评论吗？", {
					time : 0, // 20s后自动关闭
					icon : 3,
					shade : 0.3,
					btn : [ '确定', '取消' ],
					yes : function(index) {
						layer.close(index);
						var index = layer.msg('正在处理，请稍候。。。', {
							icon : 16,
							shade : 0.3
						});
						$.ajax({
							type : "GET",
							cache : false,
							data : {
								id : $tr.data("data").id,
								content : $tr.data("data").content
							},
							dataType : "json",
							url : "/manage/article/comment/delete",
							success : function(data) {
								// 关闭加载框
								layer.close(index);
								if (data != undefined && data.isSuccess == true) {
									// 刷新
									$("#pf-video-comment-table").bootstrapTable("refresh");
								} else {
									layer.msg(data.message, {
										icon : 2,
										shade : 0.3,
										btn : [ '确定' ]
									});
								}
							}
						});

					}
				});
			});
		},
		/** 关闭文章评论按钮事件 */
		_reg_closed_comment_btn : function() {
			$(document).on("click", ".pf-closed-comment-btn", function() {
				var $tr = $(this).closest("tr");
				if ($tr.data("data").closed == true) {
					var msg = "确定开启评论内容为：" + $tr.data("data").content + "的评论吗？";
				} else {
					var msg = "确定关闭评论内容为：" + $tr.data("data").content + "的评论吗？";
				}
				layer.msg(msg, {
					time : 0, // 20s后自动关闭
					icon : 3,
					shade : 0.3,
					btn : [ '确定', '取消' ],
					yes : function(index) {
						layer.close(index);
						var index = layer.msg('正在处理，请稍候。。。', {
							icon : 16,
							shade : 0.3
						});
						$.ajax({
							type : "GET",
							cache : false,
							data : {
								id : $tr.data("data").id,
								content : $tr.data("data").content
							},
							dataType : "json",
							url : "/manage/article/comment/close",
							success : function(data) {
								// 关闭加载框
								layer.close(index);
								if (data != undefined && data.isSuccess == true) {
									// 刷新
									$("#pf-article-comment-table").bootstrapTable("refresh");
								} else {
									layer.msg(data.message, {
										icon : 2,
										shade : 0.3,
										btn : [ '确定' ]
									});
								}
							}
						});

					}
				});
			});
		},

		/** 初始化评价表格 */
		_init_table : function() {
			$("#pf-article-comment-table").bootstrapTable({
				method : 'get',
				url : "/manage/article/comment/list",
				queryParams : function(params) {
					return params;
				},
				cache : false,
				// height : 400,
				sidePagination : 'server', // client or server
				queryParamsType : 'pageSize',
				// search : true,
				striped : true,
				pagination : true,
				pageSize : 10,
				pageList : [ 10, 20, 50, 100, 200 ],
				paginationDetail : true,
				paginationHAlign : 'right', // right, left
				showColumns : true,
				showRefresh : true,
				minimumCountColumns : 2,
				checkboxHeader : false,
				clickToSelect : true,
				columns : [ {
					field : 'id',
					title : '#id',
					align : 'left',
					valign : 'middle',
					visible : true,
					sortable : false
				}, {
					field : 'title',
					title : '文章名称',
					align : 'left',
					valign : 'middle',
					visible : true,
					sortable : false,
					formatter : function(value, row, index) {
						return "<span>" + row.title + "</span>";
					}
				}, {
					field : 'uname',
					title : '用户名称',
					align : 'left',
					valign : 'middle',
					visible : true,
					sortable : false,
					formatter : function(value, row, index) {
						return "<span>" + row.uname + "</span>";
					}
				}, {
					field : 'content',
					title : '评论内容',
					align : 'left',
					valign : 'middle',
					visible : true,
					sortable : false,
					formatter : function(value, row, index) {
						return "<span>" + row.content + "</span>";
					}
				}, {
					field : 'closed',
					title : '关闭状态',
					align : 'center',
					valign : 'middle',
					visible : true,
					sortable : true,
					formatter : function(value, row, index) {
						if (row.closed == true) {
							return "<span>已关闭</span>"
						} else {
							return "<span>正常</span>"
						}
					}
				}, {
					field : 'createTime',
					title : '发表时间',
					align : 'center',
					valign : 'middle',
					visible : true,
					sortable : true,
					formatter : function(value, row) {
						if (row.createTime == null || row.createTime == "") {
							// 时间戳转Date 格式化
							var dateline = new Date();
							var stime = row.dateline;
							dateline.setTime(stime * 1000);
							return dateline.format('yyyy-MM-dd hh:mm:ss');
						} else {
							return row.createTime;
						}
					}
				}, {
					field : 'option',
					title : '操作',
					align : 'center',
					valign : 'middle',
					visible : true,
					sortable : false,
					formatter : function(value, row, index) {
						var button = "";
						if (row.closed == true) {
							button += '<button type="button" class="btn btn-success btn-xs pf-closed-comment-btn"><i class="glyphicon glyphicon-ok"></i>开启</button>&nbsp;';
						} else {
							button += '<button type="button" class="btn btn-danger btn-xs pf-closed-comment-btn"><i class="glyphicon glyphicon-remove"></i>关闭</button>&nbsp;';
						}

						button += ' <button type="button" class="btn btn-danger btn-xs pf-delete-comment-btn"><i class="glyphicon glyphicon-trash"></i>删除</button>&nbsp;';
						return button;
					}
				} ]
			});
		}
	}
	ArticleCommentObject.init();

	Date.prototype.format = function(format) {
		var date = {
			"M+" : this.getMonth() + 1,
			"d+" : this.getDate(),
			"h+" : this.getHours(),
			"m+" : this.getMinutes(),
			"s+" : this.getSeconds(),
			"q+" : Math.floor((this.getMonth() + 3) / 3),
			"S+" : this.getMilliseconds()
		};
		if (/(y+)/i.test(format)) {
			format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
		}
		for ( var k in date) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
			}
		}
		return format;
	}
})(jQuery);